import { Button, Tooltip, InputNumber, Input, Form, TreeSelect } from 'antd';
import { getColorTextLoadMap, onMouseOutBtn, removeDateHmsStr, renderTableText, tableSorter, tooltipLength, priceFormat, getTenantID } from '@/utils';
import { PlusCircleOutlined, DeleteOutlined, AppstoreAddOutlined } from '@ant-design/icons';
import pageStyles from '@/common/pageLayout.less';
import { DictionaryRenderFun } from '../../Acommon';
import { formatDecimalFour } from '../../../Acommon';
import { handleNotPostion } from '@/pages/ContractController/ContractManage/mconfig';
import { ILoadMapTypeAll } from '@/common';

/** 领用/退库 - 材料 */
const materialTableColumn = ({
  openMaterialModalFun = null,
  delSelectRow = null,
  pageType,
  loadMapInfo = null,
  mapLoading = null,
  queryType = '1',
  editState,
  pageTypeOA,
}: any) => {
  const isDetail: boolean = pageType == 'detail' && !editState; // 是否是查看
  const columnConfig: Record<string, any>[] = [
    {
      title: '材料编号',
      dataIndex: 'materialCode',
      key: 'materialCode',
      width: 120,
      fixed: 'left',
      ellipsis: {
        showTitle: true,
      },
      visible: true,
      unDrag: true,
      sorter: (a: any, b: any) => (a.materialCode > b.materialCode ? 1 : -1),
      render: (text: string, record: Record<string, any>) => {
        return (
          <Tooltip title={tooltipLength(text, 50)}>
            <span>{text || '--'}</span>
          </Tooltip>
        );
      },
    },
    {
      title: '材料名称',
      dataIndex: 'assetName',
      key: 'assetName',
      width: 120,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetName'),
      render: (text: string, record: any) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '材料分类',
      dataIndex: 'assetClassName',
      key: 'assetClassName',
      width: 120,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetClassName'),
      render: (text: number) => DictionaryRenderFun({ text, isState: false }),
    },
    {
      title: '品牌',
      dataIndex: 'brandType',
      key: 'brandType',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => (a.brandType > b.brandType ? 1 : -1),
      render: (text: string) => DictionaryRenderFun({ text, isState: false }),
    },
    {
      title: '规格型号',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'paramsInfo'),
      render: (text: string, record: any) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    },
    {
      title: '供应商',
      dataIndex: 'supplier',
      key: 'supplier',
      width: 150,
      // ishidden: (queryType == '2' ? true : false),
      visible: true,
      // sorter: (a: any, b: any) => tableSorter(a?.assetjson, b?.assetjson, 'supplier'),
      // render: (text: string, record: Record<string, any>) => renderTableText(record?.assetjson?.supplier, true),
      sorter: (a: any, b: any) => tableSorter(a, b, 'supplier'),
      render: (text: string, record) => renderTableText(text, true),
    },
    {
      title: '数量',
      dataIndex: 'residueNum',
      key: 'residueNum',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => (a.residueNum > b.residueNum ? 1 : -1),
      render: (text: string, record) => {
        if (isDetail) {
          return (
            <Tooltip title={record?.expectNum + record?.quantityOrArea}>
              <div className={pageStyles['over-flow-hidden']}>{record?.expectNum + record?.quantityOrArea || '--'}</div>
            </Tooltip>
          );
        } else {
          return (
            <Tooltip title={text}>
              <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
            </Tooltip>
          );
        }
      },
    },
    {
      title: '计量单位',
      dataIndex: 'unitOfMeasurementName',
      key: 'unitOfMeasurementName',
      width: 140,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitOfMeasurement'),
      render: (text: string) => DictionaryRenderFun({ text, isState: false }),
    },

    {
      title: '单价(元)',
      dataIndex: 'assetValue',
      key: 'assetValue',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetValue'),
      render: (text: string | number) => formatDecimalFour(text),
    },
  ];

  if (queryType == '1' || queryType == '3') {
    let str = queryType == '1' ? '领用' : '借用';
    columnConfig?.push({
      title: str + '数量',
      dataIndex: 'quantityOrArea',
      key: 'quantityOrArea',
      width: 130,
      fixed: 'right',
      sorter: (a: any, b: any) => tableSorter(a, b, 'quantityOrArea'),
      render: (text: string | number) => {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
      },
    });
  }

  if (pageTypeOA == 'edit' || pageTypeOA == 'add' || (!pageTypeOA && (pageType == 'edit' || pageType == 'add'))) {
    columnConfig?.push({
      title: (
        <div style={{ width: '100%', textAlign: 'center' }}>
          <Button type="link" size="small" icon={<AppstoreAddOutlined />} className="manageBtntTxtStyle" onClick={openMaterialModalFun} disabled={isDetail}>
            选择
          </Button>
        </div>
      ),
      dataIndex: 'otherBtn',
      key: 'otherBtn',
      visible: true,
      width: 80,
      fixed: 'right',
      render: (_text: any, record: any) => {
        return (
          <div className="counterpoartDelBtnStyle">
            <Button
              type="link"
              size="small"
              icon={<DeleteOutlined />}
              className={'manageBtntTxtStyle'}
              onMouseOut={onMouseOutBtn}
              onClick={() => delSelectRow(record)}
            >
              删除
            </Button>
          </div>
        );
      },
    });
  }
  return columnConfig;
};

export { materialTableColumn };
